package com.model.demo.spring.framework.webmvc.servlet;

import java.io.File;
import java.util.Locale;

//设计这个类的主要目的是：
//1、讲一个静态文件变为一个动态文件
//2、根据用户传送参数不同，产生不同的结果
//最终输出字符串，交给 Response 输出
public class MyViewResolve {

    private String viewName;
    private File templateRootDir ;
    private String DEFAULT_TMPLATE_SUFFIX = ".html";


    public MyViewResolve(String templateRoot) {
        String templateRootPath = this.getClass().getClassLoader().getResource(templateRoot).getFile();
        this.templateRootDir = new File(templateRootPath);
    }

    public MyView resolveViewName(String viewName, Locale locale){
        this.viewName = viewName;
        if(viewName == null || "".equals(viewName.trim())){return null ;}
        viewName = viewName.endsWith(DEFAULT_TMPLATE_SUFFIX) ? viewName : viewName.concat(DEFAULT_TMPLATE_SUFFIX);
        File templateFile = new File(templateRootDir.getPath().concat(viewName).replaceAll("/+", "/"));
        return new MyView(templateFile);

    }

    public String getViewName() {
        return viewName;
    }
}
